/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONObject;

/**
 *
 * @author banhbaochay
 */
public class AnalyzeAlbum {
    
    /**
     * 
     * @param albumJSON
     * @return Map has key is album 's name and value is an album type
     */
    public static Map getAlbumMap(JSONObject albumJSON) {
        Album album = new Album(albumJSON);
        /*
         * albumList stores all albums from albumJSON
         */
        Map albumMap = new HashMap<String, Album>();
        albumMap.put(album.getName(), album);
        /*
         * waitingList stores albums which has sub albums and unchecked
         */
        List<Album> waitingList = new ArrayList<Album>();
        waitingList.add(album);
        while (waitingList.size() > 0) {
            Album currentAlbum = waitingList.get(0);
            waitingList.remove(currentAlbum);
            if (currentAlbum.hasSubAlbum()) {
                List<Album> subAlbumList = currentAlbum.getSubAlbumList();
                for (Album subAlbum : subAlbumList) {
                    albumMap.put(subAlbum.getName(), subAlbum);
                    if (subAlbum.hasSubAlbum()) {
                        waitingList.add(subAlbum);
                    }// end if subAlbum.hasSubAlbum()
                }// end for subAlbum
            }
        }// end while waitingList.size > 0
        return albumMap;
    }// end getAlbumMap method
}// end AnalyzeAlbum class

